function toggleMobileMenu() {
    let element = document.getElementById('site-menus');
    if (element.classList.contains('mobileShow')) {
        element.classList.remove('mobileShow');
    } else {
        element.classList.add('mobileShow');
    }
}

function toggleSearchInput() {
    let element = document.getElementById('search-input');
    if (element.classList.contains('show')) {
        element.classList.remove('show');
    } else {
        element.classList.add('show');
        element.focus();
    }
}

window.onload = function () {
    window.addEventListener('scroll', function () {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        if (scrollTop > 150) {
            document.getElementById('back-top').style.display = 'block';    // 显示回到顶部按钮
        } else {
            document.getElementById('back-top').style.display = 'none';     // 隐藏回到顶部按钮
        }
    });
}

window.onbeforeunload = function () {
    window.removeEventListener('scroll', function () {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        if (scrollTop > 150) {
            document.getElementById('back-top').style.display = 'block';    // 显示回到顶部按钮
        } else {
            document.getElementById('back-top').style.display = 'none';     // 隐藏回到顶部按钮
        }
    });
}

// 点击回到顶部
function getTop() {
    let timer = setInterval(() => {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        let speed = Math.ceil(scrollTop / 5);
        document.documentElement.scrollTop = scrollTop - speed;
        if (scrollTop === 0) {
            clearInterval(timer);
        }
    }, 20);
}

// 搜索文章
function searchArticle(event) {
    if (event.keyCode == 13) {
        let keyword = document.getElementById('search-input').value.trim();
        if (keyword == '') {
            return;
        }
        // 发送搜索请求
        window.location.href = `/search?keyword=${keyword}`;
    }
}